package controller

import (
	"github.com/labstack/echo"
	"github.com/sirupsen/logrus"
	"net/http"
	"src/config"
	"src/controller/param"
	"src/model"
	"src/util/context"
	"src/util/log"
	"strconv"
)

var forumpostLogger = log.GetLogger()

func writeforumpostLoggerLoggerLog(funcName, errMsg string, err error) {
	forumpostLogger.WithFields(logrus.Fields{
		"package":  config.LogMessage,
		"file":     "forumpost.go",
		"function": funcName,
		"err":      err,
	}).Warn(errMsg)
}

func CreatForumpost(c echo.Context) error {
	post := model.ForumPost{}
	err := c.Bind(&post)
	if err != nil {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	if post.Text == "" {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorContent, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorContent)
	}
	flag, err := model.PostWeixinMsgSecCheck(post.Text)
	if flag == false {
		writeforumpostLoggerLoggerLog("CreatForumpost", "内容违规1", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "内容违规")
	}
	if err != nil {
		writeforumpostLoggerLoggerLog("CreatForumpost", "内容违规2", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "内容违规")
	}

	id := context.GetJWTUserOpenid(c)
	post.UserID = id

	post, err = model.CreateForumPost(post)
	if err != nil {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorForumpostCreat, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostCreat)
	}

	err = model.AddForumpost(post.ID.Hex(), post.UserID)
	if err != nil {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorForumpostCreat, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostCreat+"："+err.Error())
	}
	resData := map[string]interface{}{}
	resData["Forumpost"] = post
	return context.RetData(c, resData)
}

func CreatForumpostTest(c echo.Context) error {
	post := model.ForumPost{}
	err := c.Bind(&post)
	if err != nil {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	if post.Text == "" {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorContent, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorContent)
	}

	id := context.GetJWTUserOpenid(c)
	if id != "123456" {
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "")
	}
	post.UserID = id

	post, err = model.CreateForumPost1(post)
	if err != nil {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorForumpostCreat, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostCreat)
	}

	err = model.AddForumpost(post.ID.Hex(), post.UserID)
	if err != nil {
		writeforumpostLoggerLoggerLog("CreatForumpost", config.ErrorForumpostCreat, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostCreat+"："+err.Error())
	}

	//resData := map[string]interface{}{}
	//resData["Forumpost"] = post
	//return context.RetData(c, resData)
	return context.RetData(c, "")
}

func DeleteForumpost(c echo.Context) error {
	postid := param.Id{}
	err := c.Bind(&postid)
	if err != nil {
		writeforumpostLoggerLoggerLog("DeleteForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	id := context.GetJWTUserOpenid(c)

	err = model.DeleteForumPost(postid.Id, id, false)
	if err != nil {
		writeforumpostLoggerLoggerLog("DeleteForumpost", config.ErrorForumpostDelete, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostDelete+err.Error())
	}

	//query := bson.M{
	//	"forum_post_id":postid.Id,
	//}
	//_ = model.DeleteMessageAll(query)

	err = model.DeleteUser_Forumpost(postid.Id, id)
	if err != nil {
		writeforumpostLoggerLoggerLog("DeleteForumpost", "更新用户数据失败", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "更新用户数据失败"+err.Error())
	}

	var reply []model.Reply
	reply, err = model.GetReplyByPostALL(postid.Id)
	for i := 0; i < len(reply); i++ {
		_, _ = model.DeleteReply(reply[i].ID.Hex(), reply[i].UserID, true)
	}

	var comment []model.Comment
	comment, err = model.GetCommentByForumpostALL(postid.Id)
	for i := 0; i < len(comment); i++ {
		_, _ = model.DeleteComment(comment[i].ID.Hex(), comment[i].UserID, true)
	}

	return context.RetData(c, "")
}

func StarForumpost(c echo.Context) error {
	action := param.Star{}
	err := c.Bind(&action)
	if err != nil {
		writeforumpostLoggerLoggerLog("StarForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}

	user_id := context.GetJWTUserOpenid(c)
	err = model.StarForumPost(action.Id, user_id, action.Action == 1)
	if err != nil {
		writeforumpostLoggerLoggerLog("StarForumpost", config.ErrorForumpostStar, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostStar)
	}

	f, err := model.GetForumpostByid(action.Id, user_id)
	if err != nil {
		writeforumpostLoggerLoggerLog("StarForumpost", "Hasnot Exist", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "Hasnot Exist")
	}

	if user_id != f.UserID {
		M := model.Message{}
		M.Type = 12
		M.ForumPostId = action.Id
		M.ActionUserId = user_id
		M.UserID = f.UserID
		M.ContentId = action.Id
		if action.Action == 1 {
			err = model.CreatMessage(M)
			if err != nil {
				writeforumpostLoggerLoggerLog("StarForumpost", config.ErrorMessageCreat, err)
				return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMessageCreat)
			}
			err = model.UpdateForumpostScore(action.Id, config.StarScore)
		} else {
			err = model.DeleteMessage(M)
			if err != nil {
				writeforumpostLoggerLoggerLog("StarForumpost", config.ErrorMessageDelete, err)
				return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMessageDelete)
			}
			err = model.UpdateForumpostScore(action.Id, -config.StarScore)
		}
		if err != nil {
			writeforumpostLoggerLoggerLog("StarForumpost", config.ErrorUpdateForumpostScore, err)
			return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorUpdateForumpostScore)
		}
	}

	return context.RetData(c, "")
}

func TipoffForumpost(c echo.Context) error {
	action := param.Tipoff{}
	err := c.Bind(&action)
	if err != nil {
		writeforumpostLoggerLoggerLog("TipoffForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}

	user_id := context.GetJWTUserOpenid(c)

	Tipoff := model.Tipoff{
		User_id:    user_id,
		Context_id: action.Id,
		Text:       action.Text,
		Level:      0,
	}

	Tipoff, err = model.CreateTipoff(Tipoff)
	if err != nil {
		writeforumpostLoggerLoggerLog("TipoffForumpost", "举报失败", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "举报失败")
	}

	err = model.UpdateForumpostScore(action.Id, -config.TipoffScore)
	if err != nil {
		writeforumpostLoggerLoggerLog("TipoffForumpost", config.ErrorUpdateForumpostScore, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorUpdateForumpostScore)
	}
	return context.RetData(c, "")
}

func GetForumpostByid(c echo.Context) error {
	postid := param.Id{}
	postid.Id = c.QueryParam("id")
	var err error
	if postid.Id == "" {
		writeforumpostLoggerLoggerLog("GetForumpostByid", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	user_id := context.GetJWTUserOpenid(c)
	resData := map[string]interface{}{}
	resData["Forumpost"], err = model.GetForumpostByid(postid.Id, user_id)
	if err != nil {
		writeforumpostLoggerLoggerLog("GetForumpostByid", config.ErrorForumpostGet, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostGet)
	}
	return context.RetData(c, resData)

}

func GetForumpost_Notoken(c echo.Context) error {
	GetForumpost := param.GetForumpost{}
	err := c.Bind(&GetForumpost)
	GetForumpost.Page, err = strconv.Atoi(c.QueryParam("page"))
	GetForumpost.PerPage, err = strconv.Atoi(c.QueryParam("per_page"))
	GetForumpost.Method, err = strconv.Atoi(c.QueryParam("method"))
	GetForumpost.Tags, err = strconv.Atoi(c.QueryParam("tags"))
	GetForumpost.Key = c.QueryParam("key")
	GetForumpost.T, err = strconv.Atoi(c.QueryParam("type"))
	if err != nil || GetForumpost.Page == 0 || GetForumpost.PerPage <= 0 || GetForumpost.T == 0 || GetForumpost.Method == 0 {
		writeforumpostLoggerLoggerLog("GetForumpost_Notoken", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}

	user_id := ""
	resData := map[string]interface{}{}
	if GetForumpost.Method == 1 {
		resData["Forumpost"], err = model.GetForumpost_time(GetForumpost.T, GetForumpost.Key, GetForumpost.Tags, GetForumpost.Page, GetForumpost.PerPage, user_id)
	} else {
		resData["Forumpost"], err = model.GetForumpost_score(GetForumpost.T, GetForumpost.Key, GetForumpost.Tags, GetForumpost.Page, GetForumpost.PerPage, user_id)

	}
	if err != nil {
		writeforumpostLoggerLoggerLog("GetForumpost_Notoken", config.ErrorForumpostGetByType, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostGetByType)
	}
	return context.RetData(c, resData)
}

func GetForumpost(c echo.Context) error {
	GetForumpost := param.GetForumpost{}
	err := c.Bind(&GetForumpost)
	GetForumpost.Page, err = strconv.Atoi(c.QueryParam("page"))
	GetForumpost.PerPage, err = strconv.Atoi(c.QueryParam("per_page"))
	GetForumpost.Method, err = strconv.Atoi(c.QueryParam("method"))
	GetForumpost.Tags, err = strconv.Atoi(c.QueryParam("tags"))
	GetForumpost.Key = c.QueryParam("key")
	GetForumpost.T, err = strconv.Atoi(c.QueryParam("type"))
	if err != nil || GetForumpost.Page == 0 || GetForumpost.PerPage <= 0 || GetForumpost.T == 0 || GetForumpost.Method == 0 {
		writeforumpostLoggerLoggerLog("GetForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}

	user_id := context.GetJWTUserOpenid(c)
	resData := map[string]interface{}{}
	if GetForumpost.Method == 1 {
		resData["Forumpost"], err = model.GetForumpost_time(GetForumpost.T, GetForumpost.Key, GetForumpost.Tags, GetForumpost.Page, GetForumpost.PerPage, user_id)
	} else {
		resData["Forumpost"], err = model.GetForumpost_score(GetForumpost.T, GetForumpost.Key, GetForumpost.Tags, GetForumpost.Page, GetForumpost.PerPage, user_id)

	}
	if err != nil {
		writeforumpostLoggerLoggerLog("GetForumpost", config.ErrorForumpostGetByType, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorForumpostGetByType)
	}
	return context.RetData(c, resData)
}

func RecommendForumpost(c echo.Context) error {
	Recommed := param.Application{}
	err := c.Bind(&Recommed)
	if err != nil {
		writeforumpostLoggerLoggerLog("RecommendForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	user_id := context.GetJWTUserOpenid(c)

	a := model.Application{}
	a.Forumpost_id = Recommed.Id
	a.Recommed_type = Recommed.Type
	a.Recommed_userid = user_id
	a, err = model.CreateApplication(a)
	if err != nil {
		writeapplicationLoggerLog("CreatApplication", "CreatApplication", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorApplicationCreat+err.Error()+a.Forumpost_id)
	}

	err = model.UpdateForumpostRecommend(Recommed.Id, user_id)
	if err != nil {
		writeforumpostLoggerLoggerLog("RecommendForumpost", config.ErrorRecommendForumpost, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorRecommendForumpost+err.Error())
	}

	return context.RetData(c, "")
}

func GetForumpostByUserid(c echo.Context) error {
	Page, err := strconv.Atoi(c.QueryParam("page"))
	PerPage, err := strconv.Atoi(c.QueryParam("per_page"))
	//id := c.QueryParam("id")
	if err != nil || Page == 0 || PerPage <= 0 {
		writeforumpostLoggerLoggerLog("GetForumpostByUserid", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	user_id := context.GetJWTUserOpenid(c)
	forumposts, err := model.GetForumpostByUserid(user_id, Page, PerPage, user_id)
	if err != nil {
		writeforumpostLoggerLoggerLog("GetForumpostByUserid", "GetForumpostByUserid-model.get", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "GetForumpostByUserid")
	}
	resData := map[string]interface{}{}
	if len(forumposts) == 0 {
		resData["Forumpost"] = []model.ForumPost{}
	} else {
		resData["Forumpost"] = forumposts
	}
	return context.RetData(c, resData)
}

func ShareForumpost(c echo.Context) error {
	id := param.Id{}
	err := c.Bind(&id)
	if err != nil {
		writeforumpostLoggerLoggerLog("ShareForumpost", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	err = model.UpdateForumpostScore(id.Id, config.ShareScore)
	if err != nil {
		writeforumpostLoggerLoggerLog("ShareForumpost", config.ErrorUpdateForumpostScore, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorUpdateForumpostScore)
	}
	return context.RetData(c, "")
}
